﻿Imports Abyx.SVCMS.Library
Imports Abyx.SVCMS.Library.DB
Imports System.IO

Public Class LicenseTool_Form

    Private Sub LicenseTool_Form_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        LoadSettings()
    End Sub
    Private Sub LicenseTool_Form_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        SaveSettings()
    End Sub

#Region "License Tab"
    Private Sub SofwareLogo_Button_Click(sender As System.Object, e As System.EventArgs) Handles LogoBanner_Button.Click
        If Logo_OpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            LogoBanner_PictureBox.Image = Image.FromFile(Logo_OpenFileDialog.FileName)
        Else
            LogoBanner_PictureBox.Image = Nothing
        End If
    End Sub
    Private Sub ReportLogo_Button_Click(sender As System.Object, e As System.EventArgs) Handles LogoReport_Button.Click
        If Logo_OpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            LogoReport_PictureBox.Image = Image.FromFile(Logo_OpenFileDialog.FileName)
        Else
            LogoReport_PictureBox.Image = Nothing
        End If
    End Sub
    Private Sub LicenseLoad_Button_Click(sender As System.Object, e As System.EventArgs) Handles LicenseLoad_Button.Click

        Dim license As LicenseInfoType = SysManager.License_Read
        CompanyName_TextBox.Text = license.CompanyName
        Address_TextBox.Text = license.Address
        Contacts_TextBox.Text = license.Contacts
        LogoBanner_PictureBox.Image = license.Logo_Banner_Image
        LogoReport_PictureBox.Image = license.Logo_Report_Image
        LicenseUpdatedDate_Label.Text = SysManager.DateFormatDisplay(license.LMDate)
        MsgBox("License loaded")

    End Sub
    Private Sub LicenseUpdate_Button_Click(sender As System.Object, e As System.EventArgs) Handles LicenseUpdate_Button.Click
        If CompanyName_TextBox.Text = "" Then
            MsgBox("Company name cannot be empty", MsgBoxStyle.Critical)
            Exit Sub
        End If
        LicenseUpdatedDate_Label.Text = SysManager.DateFormatDisplay(DateTime.Now)
        Dim license As New LicenseInfoType
        With license
            .CompanyName = CompanyName_TextBox.Text
            .Address = Address_TextBox.Text
            .Contacts = Contacts_TextBox.Text
            .LMDate = DateTime.Now
            If Not LogoBanner_PictureBox.Image Is Nothing Then
                Dim ms As New MemoryStream
                LogoBanner_PictureBox.Image.Save(ms, Imaging.ImageFormat.Png)
                .Logo_Banner = ms.GetBuffer
            Else
                .Logo_Banner = Nothing
            End If
            If Not LogoReport_PictureBox.Image Is Nothing Then
                Dim ms As New MemoryStream
                LogoReport_PictureBox.Image.Save(ms, Imaging.ImageFormat.Png)
                .Logo_Report = ms.GetBuffer
            Else
                .Logo_Report = Nothing
            End If
        End With
        SysManager.License_Write(license)
        MsgBox("License has been updated", MsgBoxStyle.Information)
    End Sub
#End Region

#Region "Settings Tab"
    Private Sub LoadSettings()
        SettingDbConnection_SqlConnectionStringBuilder.ConnectionString = SysManager.GetConnectionString
    End Sub
    Private Sub SaveSettings()
        If Me.SettingDbConnection_SqlConnectionStringBuilder.ConnectionString <> "" Then
            SysManager.SetConnectoinString(Me.SettingDbConnection_SqlConnectionStringBuilder.ConnectionString)
        End If
    End Sub
    Private Sub SettingDbConnection_SqlConnectionStringBuilder_ConnectionStringChanged(sender As System.Object, e As System.EventArgs) Handles SettingDbConnection_SqlConnectionStringBuilder.ConnectionStringChanged
        SaveSettings()
    End Sub
#End Region

End Class
